﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Age.Qna.Site.Mvc.Controllers
{
    public class QuestionController : Controller
    {
        //
        // GET: /Question/

        public ActionResult Index()
        {
            return View();
        }
        
        // 
        // GET:

        public ActionResult Display()
        {
          return View();
        }

        public ActionResult Rand()
        {
          Qnawsvc.QnaWSvcClient client = new Qnawsvc.QnaWSvcClient("ws");
          var entity = client.GetRandomQuestion();
          client.Close();
          return View(entity);
        }

        //
        // GET: /Question/Create

        public ActionResult New()
        {

            return View();
        } 

        

        //
        // POST: /Question/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        [HttpPost]
        public ActionResult Save(string data)
        {

					try
					{
						Qnawsvc.QnaEntity entity = new Qnawsvc.QnaEntity();

						JavaScriptSerializer x = new JavaScriptSerializer();
						
						entity = x.Deserialize<Qnawsvc.QnaEntity>(data);

						Qna.Site.Mvc.Models.Question q = new Models.Question();
						
						// Save the entity
						int saved = q.Save(entity);

						if (saved > 0)
						{
							return Content("Question saved. <span>Click here to create a new question.</span>");
						}
						else
						{
							return Content("Error. Please try again.");
						}


					}
					catch (Exception ex)
					{
						return Content(ex.Message);
					}
        }

				public ActionResult Pop()
				{
					return View();
				}
    }
}
